{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6321595e",
   "metadata": {},
   "source": [
    "# Cox生存分析\n",
    "\n",
    "* `mydir`：自己的数据\n",
    "* `ostime_column`: 数据对应的生存时间，不一定非的是OST，也可以是DST、FST等。\n",
    "* `os`：生存状态，不一定非的是OS，也可以是DS、FS等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0fc498",
   "metadata": {},
   "outputs": [],
   "source": [
    "from lifelines import CoxPHFitter\n",
    "import pandas as pd\n",
    "from onekey_algo import OnekeyDS as okds\n",
    "\n",
    "mydir = okds.survival\n",
    "ostime_column = 'duration'\n",
    "os_column = 'result'\n",
    "data = pd.read_csv(mydir)\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0201324",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "cph = CoxPHFitter()\n",
    "cph.fit(data, duration_col=ostime_column, event_col=os_column)\n",
    "\n",
    "cph.print_summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "349aa141",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "cph.plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dcf51b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cph.plot_partial_effects_on_outcome(covariates='Tstage', values=[0, 1, 2, 3], cmap='coolwarm')\n",
    "plt.xlim(20, 83)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea523994",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
